package com.andox.fn.inf.rpc.rest.protocol.registry;

import java.util.List;
import java.util.Map;

import com.andox.fn.inf.rpc.rest.config.ReferenceConfig;
import com.andox.fn.inf.rpc.rest.config.ServiceConfig;




/**   
 * @ClassName:   DefaultRegistryService   
 * @author:      Andox
 * @date:        2019年8月16日 上午11:15:07   
 *     
 */
public abstract class AbstractRegistryService implements RegistryService {
	/**
	 * provider register
	 */
	protected Map<String, ServiceConfig> registerMap;
	
	/**
	 * consumer subscribe
	 */
	protected Map<String, ReferenceConfig> consumerMap;
	
	protected abstract List<ReferenceConfig> doSubsribe(ReferenceConfig referenceConfig, RegistryNotify rn);
	protected abstract void doUnsubsribe(ReferenceConfig referenceConfig);
	protected abstract void doRegister(ServiceConfig serviceConfig);
	protected abstract void doUnregister(ServiceConfig serviceConfig);
	
	@Override
	public List<ReferenceConfig> subscribe(ReferenceConfig referenceConfig, RegistryNotify rn) {
		
		return this.doSubsribe(referenceConfig, rn);
	}

	@Override
	public void unsubscribe(ReferenceConfig referenceConfig) {
		consumerMap.remove("");
		
		this.doUnsubsribe(referenceConfig);
	}

	@Override
	public void register(ServiceConfig serviceConfig) {
		this.registerMap.put("", serviceConfig);
		
		this.doRegister(serviceConfig);
	}

	@Override
	public void unregister(ServiceConfig serviceConfig) {
		this.registerMap.remove("");
		
		this.doUnregister(serviceConfig);
	}

//	private RegistryImpl findRegistryImpl(InterfaceConfig ic) {
//		if (!this.registryImplTable.containsKey(ic.getRegistry())){
//			synchronized (this) {
//				if (!this.registryImplTable.containsKey(ic.getRegistry())){
//					String protocol = RegistryUtil.findRegistryProtocol(ic.getRegistry());
//					RegistryImpl registryImpl = ExtensionLoader.getExtensionLoader(RegistryImpl.class).getExtension(protocol);
//					registryImpl.setConnection(ic.getRegistry());
//					registryImpl.start();
//				}
//			}
//		}
//		
//		return this.registryImplTable.get(ic.getRegistry());
//	}

	@Override
	public List<ServiceConfig> lookup(ReferenceConfig referenceConfig) {
		// TODO Auto-generated method stub
		return null;
	}
}
